
package server.shops;



import client.MapleCharacter;
import client.MapleClient;
import client.inventory.Item;
import client.inventory.ItemFlag;
import constants.GameConstants;
import handling.channel.ChannelServer;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import server.MapleInventoryManipulator;
import server.MapleItemInformationProvider;
import server.Timer;
import server.maps.MapleMapObjectType;
import tools.MaplePacketCreator;
import tools.packet.PlayerShopPacket;

import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.ScheduledFuture;



















 public class HiredMerchant extends AbstractPlayerStore {
    public static final Logger log = LogManager.getLogger(HiredMerchant.class);
    private final List<String> blacklist = new LinkedList();
    private final long start = System.currentTimeMillis();
    public ScheduledFuture<?> schedule = Timer.EtcTimer.getInstance().schedule(() -> {
        if (this.getMCOwner() != null && this.getMCOwner().getPlayerShop() == this) {
            this.getMCOwner().setPlayerShop((IMaplePlayerShop)null);
        }

        this.removeAllVisitors(-1, -1);
        this.closeShop(true, true);
    }, 604800000L);
    private int storeid;
    private long lastChangeNameTime = 0L;

    public HiredMerchant(MapleCharacter owner, int itemId, String desc) {
        super(owner, itemId, desc, "", 6);
    }











    @Override
    public byte getShopType() {
        /*  46 */
        return 1;

    }



    public void setStoreid(int storeid) {
        /*  50 */
        this.storeid = storeid;

    }



    public List<MaplePlayerShopItem> searchItem(int itemSearch) {
        /*  54 */
        List<MaplePlayerShopItem> itemz = new LinkedList<>();
        /*  55 */
        for (MaplePlayerShopItem item : this.items) {
            /*  56 */
            if (item.item.getItemId() == itemSearch && item.bundles > 0) {
                /*  57 */
                itemz.add(item);

            }

        }
        /*  60 */
        return itemz;

    }




    @Override
    public void buy(MapleClient c, int item, short quantity) {
        /*  65 */
        MaplePlayerShopItem pItem = this.items.get(item);
        /*  66 */
        Item shopItem = pItem.item;
        /*  67 */
        Item newItem = shopItem.copy();
        /*  68 */
        short perbundle = newItem.getQuantity();
        /*  69 */
        long theQuantity = pItem.price * quantity;
        /*  70 */
        newItem.setQuantity((short) (quantity * perbundle));

        /*  72 */
        short flag = newItem.getFlag();

        /*  74 */
        if (ItemFlag.可以交换1次.check(flag)) {
            /*  75 */
            newItem.setFlag((short) (flag - ItemFlag.可以交换1次.getValue()));
            /*  76 */
        } else if (ItemFlag.宿命剪刀.check(flag)) {
            /*  77 */
            newItem.setFlag((short) (flag - ItemFlag.宿命剪刀.getValue()));

        }

        /*  80 */
        if (MapleInventoryManipulator.checkSpace(c, newItem.getItemId(), newItem.getQuantity(), newItem.getOwner())) {
            /*  81 */
            long gainmeso = getMeso() + theQuantity - GameConstants.EntrustedStoreTax(theQuantity);
            /*  82 */
            if (gainmeso > 0L) {
                /*  83 */
                setMeso(gainmeso);
                /*  84 */
                pItem.bundles = (short) (pItem.bundles - quantity);
                /*  85 */
                MapleInventoryManipulator.addFromDrop(c, newItem, false);
                /*  86 */
                this.bought.add(new AbstractPlayerStore.BoughtItem(newItem.getItemId(), quantity, theQuantity, c.getPlayer().getName()));
                /*  87 */
                c.getPlayer().gainMeso(-theQuantity, false);
                /*  88 */
                saveItems();
                /*  89 */
                MapleCharacter chr = getMCOwnerWorld();
                /*  90 */
                StringBuilder msg = new StringBuilder();
                /*  91 */
                MapleItemInformationProvider ii = MapleItemInformationProvider.getInstance();
                /*  92 */
                msg.append("物品");
                /*  93 */
                msg.append(ii.getName(newItem.getItemId()));
                /*  94 */
                msg.append(" 通过精灵商人售出（");
                /*  95 */
                msg.append(perbundle).append("x").append(quantity);
                /*  96 */
                msg.append("）剩余数量：");
                /*  97 */
                msg.append(pItem.bundles).append(" 购买者：").append(c.getPlayer().getName());
                /*  98 */
                if (chr != null) {
                    /*  99 */
                    chr.send(MaplePacketCreator.showRedNotice(msg.toString()));

                }
                /* 101 */
                log.info((chr != null) ? chr.getName() : getOwnerName(), "雇佣商店卖出: " + newItem.getItemId() + " - " + msg.toString() + " 价格: " + theQuantity);

            } else {
                /* 103 */
                c.getPlayer().dropMessage(1, "金币不足.");
                /* 104 */
                c.announce(MaplePacketCreator.enableActions());

            }

        } else {
            /* 107 */
            c.getPlayer().dropMessage(1, "背包已满.");
            /* 108 */
            c.announce(MaplePacketCreator.enableActions());

        }

    }




    @Override
    public void closeShop(boolean saveItems, boolean remove) {
        /* 114 */
        if (this.schedule != null) {
            /* 115 */
            this.schedule.cancel(false);

        }
        /* 117 */
        if (saveItems) {
            /* 118 */
            saveItems();
            /* 119 */
            this.items.clear();

        }
        /* 121 */
        if (remove) {
            /* 122 */
            ChannelServer.getInstance(this.channel).removeMerchant(this);
            /* 123 */
            getMap().broadcastMessage(PlayerShopPacket.destroyHiredMerchant(getOwnerId()));

        }
        /* 125 */
        getMap().removeMapObject(this);
        /* 126 */
        this.schedule = null;

    }



    public int getTimeLeft() {
        /* 130 */
        return (int) (System.currentTimeMillis() - this.start);

    }



    public int getTimeLeft(boolean first) {
        /* 134 */
        if (first) {
            /* 135 */
            return (int) this.start;

        }
        /* 137 */
        return 86400 - (int) (System.currentTimeMillis() - this.start) / 1000;

    }



    public int getStoreId() {
        /* 141 */
        return this.storeid;

    }






    public boolean canChangeName() {
        /* 148 */
        if (this.lastChangeNameTime + 60000L > System.currentTimeMillis()) {
            /* 149 */
            return false;

        }
        /* 151 */
        this.lastChangeNameTime = System.currentTimeMillis();
        /* 152 */
        return true;

    }



    public int getChangeNameTimeLeft() {
        /* 156 */
        int time = 60 - (int) (System.currentTimeMillis() - this.lastChangeNameTime) / 1000;
        /* 157 */
        return (time > 0) ? time : 1;

    }




    @Override
    public MapleMapObjectType getType() {
        /* 162 */
        return MapleMapObjectType.HIRED_MERCHANT;

    }




    @Override
    public void sendDestroyData(MapleClient client) {
        /* 167 */
        if (isAvailable()) {
            /* 168 */
            client.announce(PlayerShopPacket.destroyHiredMerchant(getOwnerId()));

        }

    }




    @Override
    public void sendSpawnData(MapleClient client) {
        /* 174 */
        if (isAvailable()) {
            /* 175 */
            client.announce(PlayerShopPacket.spawnHiredMerchant(this));

        }

    }



    public boolean isInBlackList(String bl) {
        /* 180 */
        return this.blacklist.contains(bl);

    }



    public void addBlackList(String bl) {
        /* 184 */
        this.blacklist.add(bl);

    }



    public void removeBlackList(String bl) {
        /* 188 */
        this.blacklist.remove(bl);

    }



    public void sendBlackList(MapleClient c) {
        /* 192 */
        c.announce(PlayerShopPacket.MerchantBlackListView(this.blacklist));

    }



    public void sendVisitor(MapleClient c) {
        /* 196 */
        c.announce(PlayerShopPacket.MerchantVisitorView(this.visitorsList));

    }

}


/* Location:              C:\PGX\Server.jar!\server\shops\HiredMerchant.class
 * Java compiler version: 8 (52.0)
 * JD-Core Version:       1.1.3
 */